package com.powergisol.gis.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.dto.ExcelRectangularPillarModelDTO;
import com.powergisol.gis.entity.BoardBasisEntity;
import com.powergisol.gis.entity.TabCoordinateSystem;
import com.powergisol.gis.vo.ExcelInFailedDataVo;
import com.powergisol.gis.vo.TabRectangularPillarVO;
import com.powergisol.gis.vo.TaskResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class BoardExcelModelListener extends AnalysisEventListener {

    private final static Pattern NUMBER_PATTERN = Pattern.compile("([+\\-]?[0-9]+[.]?[\\d]*)");

    private BoardBasisEntity boardBasisEntity;
    private Integer valueColumn;
    private TabRectangularPillarVO pillarVO;
    private TabCoordinateSystem system;
    private ExcelRectangularPillarModelDTO modelDTO;
    private CacheManager cacheManager;
    public BoardExcelModelListener() {
    }

    public BoardExcelModelListener(BoardBasisEntity boardBasisEntity, TabRectangularPillarVO pillarVO, TabCoordinateSystem system,
                                   ExcelRectangularPillarModelDTO modelDTO,CacheManager cacheManager) {
        this.boardBasisEntity = boardBasisEntity;
        this.pillarVO = pillarVO;
        this.system = system;
        this.modelDTO = modelDTO;
        this.cacheManager = cacheManager;
    }

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        ArrayList data = (ArrayList) o;
        try {
            if (data.isEmpty()) {
                return;
            }
            int j = 0;
            int num = data.size();
            for (int i = 0; i < num; i++) {
                if (j > 0) {
                    return;
                }
                String str = String.valueOf(data.get(i));
                if (StringUtils.isBlank(str)) continue;
                if (null == valueColumn && str.equals("字段值")) {
                    valueColumn = i;
                }
                switch (str) {
                    case "Hcdm01":
                        boardBasisEntity.setHcdm01(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "Hcdm02":
                        boardBasisEntity.setHcdm02(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "Hcdm03":
                        boardBasisEntity.setHcdm03(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "Hcdm04":
                        boardBasisEntity.setHcdm04(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "Hcdm05":
                        boardBasisEntity.setHcdm05(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "Hcdm06":
                        boardBasisEntity.setHcdm06(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB00":
                        boardBasisEntity.setLb00(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB01":
                        boardBasisEntity.setLb01(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB02":
                        boardBasisEntity.setLb02(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB03":
                        boardBasisEntity.setLb03(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB04":
                        boardBasisEntity.setLb04(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB11":
                        boardBasisEntity.setLb11(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB12":
                        boardBasisEntity.setLb12(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB13":
                        boardBasisEntity.setLb13(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB21":
                        boardBasisEntity.setLb21(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB22":
                        boardBasisEntity.setLb22(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB23":
                        boardBasisEntity.setLb23(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB24":
                        boardBasisEntity.setLb24(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB25":
                        boardBasisEntity.setLb25(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB26":
                        boardBasisEntity.setLb26(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB27":
                        boardBasisEntity.setLb27(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB30":
                        boardBasisEntity.setLb30(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "LB31":
                        boardBasisEntity.setLb31(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB32":
                        boardBasisEntity.setLb32(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB33":
                        boardBasisEntity.setLb33(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB34":
                        boardBasisEntity.setLb34(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "LB50":
                        boardBasisEntity.setLb50(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BK（1，1）":
                        boardBasisEntity.setBk1(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BK（2，1）":
                        boardBasisEntity.setBk2(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Double.parseDouble(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BK（3，1）":
                        boardBasisEntity.setBk3(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BK（4，1）":
                        boardBasisEntity.setBk4(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BK（5，1）":
                        boardBasisEntity.setBk5(valueColumn >= num ? null : data.get(valueColumn) == null ? null : new BigDecimal(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BK（6，1）":
                        boardBasisEntity.setBk6(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "BK（7，1）":
                        boardBasisEntity.setBk7(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "BKM（1，1）":
                        boardBasisEntity.setBkm51(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Integer.parseInt(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BKM（2，1）":
                        boardBasisEntity.setBkm52(valueColumn >= num ? null : data.get(valueColumn) == null ? null : Integer.parseInt(data.get(valueColumn).toString()));
                        j = j + 1;
                        break;
                    case "BKM（3，1）":
                        boardBasisEntity.setBkm53(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    case "BKM（4，1）":
                        boardBasisEntity.setBkm54(valueColumn >= num ? null : data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j + 1;
                        break;
                    default:
                        break;

                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if(boardBasisEntity.getLb21()==null || boardBasisEntity.getLb22()==null){
            return;
        }
        saveData();
    }

    private void saveData() {


        /*BigDecimal x;
        BigDecimal y;
        BigDecimal x2;
        BigDecimal y2;*/
        boardBasisEntity.setOlb21(boardBasisEntity.getLb21());
        boardBasisEntity.setOlb22(boardBasisEntity.getLb22());
        boardBasisEntity.setOlb23(boardBasisEntity.getLb23());
        boardBasisEntity.setOlb25(boardBasisEntity.getLb25());
        boardBasisEntity.setOlb26(boardBasisEntity.getLb26());
        boardBasisEntity.setOlb27(boardBasisEntity.getLb27());

        BigDecimal olb21 = mmTom(pillarVO.getLon().multiply(new BigDecimal("1000")).add(boardBasisEntity.getLb21()));
        BigDecimal olb22 = mmTom(pillarVO.getLat().multiply(new BigDecimal("1000")).add(boardBasisEntity.getLb22().add(boardBasisEntity.getBk4().divide(new BigDecimal("2")))));
        BigDecimal olb25 = mmTom(pillarVO.getLon().multiply(new BigDecimal("1000")).add(boardBasisEntity.getLb25()));
        BigDecimal olb26 = mmTom(boardBasisEntity.getLb26().subtract(boardBasisEntity.getBk4().divide(new BigDecimal("2"))).add(pillarVO.getLat().multiply(new BigDecimal("1000"))));
        BigDecimal lb23 =  mmTom(pillarVO.getHigh().multiply(new BigDecimal("1000")).add(boardBasisEntity.getLb23()));
        /*if (1 == pillarVO.getIsChange()) {
            // x/y起点
            y = XyChangeUtil.changey(olb21,olb22);
            x = XyChangeUtil.changex(olb21,olb22);
            // x/y终点
            y2 = XyChangeUtil.changey(olb25,olb26);
            x2 = XyChangeUtil.changex(olb25,olb26);
        } else {
            // x/y起点
            y = mmTom(boardBasisEntity.getLb22());//纬度
            x = mmTom(boardBasisEntity.getLb21());//经度
            // x/y终点
            y2 = mmTom(boardBasisEntity.getLb26());//纬度
            x2 = mmTom(boardBasisEntity.getLb25());//经度
        }*/
       /* if (x.compareTo(new BigDecimal("180")) > 0 ||x2.compareTo(new BigDecimal("180")) > 0 || x.compareTo(new BigDecimal("-180")) < 0 || x2.compareTo(new BigDecimal("-180")) < 0 ||
                y.compareTo(new BigDecimal("90")) > 0 ||y2.compareTo(new BigDecimal("90")) > 0 || y.compareTo(new BigDecimal("-90")) < 0 ||y2.compareTo(new BigDecimal("-90")) < 0 ) {
            dealFail("坐标Xl或坐标Yw不在范围内");
            return;
        }*/

        boardBasisEntity.setLb21(olb21);
        boardBasisEntity.setLb22(olb22);
        boardBasisEntity.setLb23(lb23);
        boardBasisEntity.setLb25(olb25);
        boardBasisEntity.setLb26(olb26);
        boardBasisEntity.setLb27(mmTom(pillarVO.getHigh().multiply(new BigDecimal(1000)).add(boardBasisEntity.getLb27())));
        modelDTO.getBasisEntities().add(boardBasisEntity);
    }

    //转换单位 (mm 转 m （毫米 转 米）)
    private BigDecimal mmTom(BigDecimal mm) {
        if (mm != null) {
            return mm.divide(new BigDecimal("1000"), 10, BigDecimal.ROUND_DOWN);
        }
        return new BigDecimal(0);
    }
    private void dealFail(String msg) {
        List<ExcelInFailedDataVo> listFailed = new ArrayList<>();
        ExcelInFailedDataVo failedDataVo = new ExcelInFailedDataVo();
        failedDataVo.setReason(msg);
        failedDataVo.setRowIndex(pillarVO.getExcelName()+"中sheet "+pillarVO.getSheetName());
        listFailed.add(failedDataVo);
        Cache cache = cacheManager.getCache("cache-data");
        TaskResultVo resultVo = new TaskResultVo();
        resultVo.setListFailed(listFailed);
        cache.put(pillarVO.getSheetName()+pillarVO.getUuid() + "board", resultVo);
    }
}
